import Taro from '@tarojs/taro'

const baseUrl = 'http://101.37.65.67';
export default {
  baseOptions(params, method = 'GET') {
    let {url, data} = params
    let contentType = 'application/json'
    contentType = params.contentType || contentType
    const option = {
      url: url.indexOf('http') !== -1 ? url : baseUrl + url,
      data: data,
      method: method,
      header: {
        'content-type': contentType,
        'Authorization': Taro.getStorageSync('Authorization')
      },
      success(res) {
        // console.log('res', res)
        // Taro.setStorageSync('Authorization', '')
      },
      error(e) {
        console.log('网络错误', e);
      }
    }
    return Taro.request(option)
  },
  get(url, data = '') {
    let option = {url, data}
    return this.baseOptions(option)
  },
  post: function (url, data, contentType) {
    let params = {url, data, contentType}
    return this.baseOptions(params, 'POST')
  },
  put(url, data = '') {
    let option = {url, data}
    return this.baseOptions(option, 'PUT')
  },
  delete(url, data = '') {
    let option = {url, data}
    return this.baseOptions(option, 'DELETE')
  }
}
